﻿using Microsoft.CodeAnalysis;

namespace AutoRevit.Generator;

internal class SymbolUtils
{
    /// <summary>
    /// 获取方法上Attribute的属性值
    /// </summary>
    /// <param name="methodSymbol"></param>
    /// <param name="attributeSymbol"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    internal static string? GetAttributePropertyValue(IMethodSymbol methodSymbol, INamedTypeSymbol attributeSymbol, string name)
    {
        AttributeData attributeData = methodSymbol.GetAttributes().Single(ad => ad.AttributeClass!.Equals(attributeSymbol, SymbolEqualityComparer.Default));

        TypedConstant transactionModeOpt = attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == name).Value;

        return transactionModeOpt.IsNull ? null : transactionModeOpt.Value?.ToString();
    }
}
